/*	Replication do-file
	Metzger, "The Chicken or the Egg?", II
	
	27AUG16
	
	
	DESCRIPTION:
		This file provides replication code to generate all the paper's tables and graphs.				
		
	REQUIRED PACKAGES:				
		* netplot (ssc install netplot)
		* pred
		* tweaks to the Clarify package's estsimp and simqi commands for suest,coev (copy everything from "ado" in replication folder to local ado folder)
			// To find loc of local ado folder, type: di "`c(sysdir_personal)'"												
	
	SOFTWARE VERSION:
		Used Stata 14.1 to generate the following tables and figures.				*/	
******************** MUST SET ********************

global dir "C:/Users/uspskm/Desktop/II replic/"	// to define the local working directory for *assembly files*
															* note: both PC and Mac need forward slash at end of path. 
	
	* Controls for behavior eq (leave as is to replicate)
	global controlsBeh polity21 lnTradeTotal1 lnCap_1 majpow1
	
	* Controls for network eq (leave as is to replicate)
	global controlsNet demL lnTradeTotal lnCincHL mp contig_d alliance_d															
															
// ^ 	Run above lines.  Each code block is written independently, and can then 
//		be run by highlighting the entire relevant block and exectuting.

*******************************************************************************	
// TABLES
**************************
// Table 1: Results of Dispute Network-Militarized Behavior Coevolution Models
{
cd "$dir"
use "data.dta", clear

	// MODEL 1: PASSIVE HPHILY ONLY (preferred specification) 
	{	
		qui logit ccode1MID_allInNet 	ccode1MID_allInNetL1 contagionIss__allInNet ///
										$controlsBeh ///
															if(stateSample==1)
		pred if(stateSample==1)
		est store beh1b_c2

		qui logit icowIss 	icowIssL1 hphilyMID_allInNet_peace ///
							$controlsNet ///
												if(newGuys==0)
		pred if(newGuys==0)
		est store net1b_c2

		suest beh1b_c2 net1b_c2	
	}
	
	// MODEL 2: BOTH HPHILY VARS
	{
		qui logit ccode1MID_allInNet 	ccode1MID_allInNetL1 contagionIss__allInNet ///
										$controlsBeh ///
															if(stateSample==1)
		pred if(stateSample==1)
		est store beh1b_c

		qui logit icowIss 	icowIssL1 hphilyMID_allInNet_mid hphilyMID_allInNet_peace ///
							$controlsNet ///
												if(newGuys==0)
		pred if(newGuys==0)
		est store net1b_c

		suest beh1b_c net1b_c
	}
}
**************************
// Table 2: Reconsidering Previous Studies
{
cd "$dir"
use "data.dta", clear

	* MODEL 3: BASIC LOGIT
	{
		logit 	 ccode1MID_allInNet 		///
										$controlsBeh ///
										totalContig1 totalIssues1 ///
															 if(stateSample==1 & ccode1MID_allInNetL1!=.)
		pred if(stateSample==1 & ccode1MID_allInNetL1!=.)
	}
	
	* MODEL 4: COEVOLUTION
	{	
		qui logit ccode1MID_allInNet	ccode1MID_allInNetL1 contagionIss__allInNet ///
										$controlsBeh ///
										totalContig1 totalIssues1 ///
															 if(stateSample==1)
		pred if(stateSample==1)
		est store beh1b_z_c2

		qui logit icowIss 	icowIssL1 hphilyMID_allInNet_peace ///
							$controlsNet ///
												if(newGuys==0)
		pred if(newGuys==0)
		est store net1b_z_c2

		suest beh1b_z_c2 net1b_z_c2	
	}
	
	* MODEL 5: NO CONTAGION
	{	
		qui logit ccode1MID_allInNet 	ccode1MID_allInNetL1 ///
										$controlsBeh ///
										totalContig1 totalIssues1 ///
															 if(stateSample==1)
		pred if(stateSample==1)
		est store beh1b_z_c2CC

		qui logit icowIss 	icowIssL1 hphilyMID_allInNet_peace ///
							$controlsNet ///
												if(newGuys==0)
		pred if(newGuys==0)
		est store net1b_z_c2CC

		suest beh1b_z_c2CC net1b_z_c2CC
	}
}
**************************
// Supplemental Table 1: Contagion Coding Example: State i, Year t
{
	* Generated by hand.
}
**************************
// Supplemental Table 2: Disputed Issue Types, by Dyad-Year
{
	cd "$dir"
	use "data.dta", clear

	// get sample flags
	qui logit icowIss 	icowIssL1 hphilyMID_allInNet_mid hphilyMID_allInNet_peace $controlsNet if(newGuys==0)
	cap drop flag
	gen flag = e(sample)
	
	// Do the table
	qui{
		count if(flag==1)
		local denom = r(N)
		
		count if(flag==1 & icowIss==0)
		noi display _continue "No active dispute|`r(N)'|"
			noi display _c %4.2f (`r(N)'/`denom') * 100
			noi display "%"
		
		count if(flag==1 & terrissMax==1 & marissMax==0 & riverissMax==0)
		noi display _continue "Territorial only|`r(N)'|"
			noi display _c %4.2f (`r(N)'/`denom') * 100
			noi display "%"
		
		count if(flag==1 & terrissMax==0 & marissMax==1 & riverissMax==0)
		noi display _continue "Maritime only|`r(N)'|"
			noi display _c %4.2f (`r(N)'/`denom') * 100
			noi display "%"
		
		count if(flag==1 & terrissMax==0 & marissMax==0 & riverissMax==1)
		noi display _continue "River only|`r(N)'|"
			noi display _c %4.2f (`r(N)'/`denom') * 100
			noi display "%"
		
		count if(flag==1 & terrissMax==1 & marissMax==1 & riverissMax==0)
		noi display _continue "Territory + Maritime|`r(N)'|"
			noi display _c %4.2f (`r(N)'/`denom') * 100
			noi display "%"
		
		count if(flag==1 & terrissMax==1 & marissMax==0 & riverissMax==1)
		noi display _continue "Territory + River|`r(N)'|"
			noi display _c %4.2f (`r(N)'/`denom') * 100
			noi display "%"
		
		count if(flag==1 & terrissMax==0 & marissMax==1 & riverissMax==1)
		noi display _continue "Maritime + River|`r(N)'|"
			noi display _c %4.2f (`r(N)'/`denom') * 100
			noi display "%"
		
		count if(flag==1 & terrissMax==1 & marissMax==1 & riverissMax==1)
		noi display _continue "All three|`r(N)'|"
			noi display _c %4.2f (`r(N)'/`denom') * 100
			noi display "%"
			
		noi display _continue "Total|`denom'|"
			noi display _c %4.2f (`denom'/`denom') * 100
			noi display "%"	
	}
}
**************************
// Supplemental Table 3: Summary Statistics
{
	cd "$dir"
	use "data.dta", clear

	// get sample flags
	qui logit icowIss icowIssL1 hphilyMID_allInNet_mid hphilyMID_allInNet_peace $controlsNet if(newGuys==0)
	cap drop flag
	gen flag = e(sample)
	
	qui logit ccode1MID_allInNet ccode1MID_allInNetL1 contagionIss__allInNet $controlsBeh totalContig1 if(stateSample==1)
	cap drop bFlag
	gen bFlag = e(sample)

	// summarize the stats.
	qui{
		* BEH eq
		noi sum ccode1MID_allInNet contagionIss__allInNet $controlsBeh if(bFlag==1)
		* NET eq
		noi sum icowIss hphilyMID_allInNet_peace $controlsNet if(flag==1)
	}
}
**************************
// Supplemental Table 4: Dyadic MID as Behavior DV
{
	cd "$dir"
	use "data.dta", clear

	qui logit dyadMID_all dyadMID_allL1 contagH contagL $controlsNet  if(newGuys==0)
	est store beh9_c2

	qui logit icowIss icowIssL1 hphilyMID_allInNet_peace $controlsNet if(newGuys==0)
	est store net9_c2

	suest beh9_c2 net9_c2
}
**************************
// Supplemental Table 5: Robustness Checks [Appendix D]
{
	cd "$dir"
	use "data.dta", clear
	
	// get sample flags
	qui logit icowIss icowIssL1 hphilyMID_allInNet_mid hphilyMID_allInNet_peace $controlsNet if(newGuys==0)
	cap drop flag
	gen flag = e(sample)
	
	qui logit ccode1MID_allInNet ccode1MID_allInNetL1 contagionIss__allInNet $controlsBeh totalContig1 if(stateSample==1)
	cap drop bFlag
	gen bFlag = e(sample)
	

	// MODEL D.1
	{
		qui logit ccode1MID_allInNet ccode1MID_allInNetL1 contagionIss__allInNet if(bFlag==1)
		est store beh1

		qui logit icowIss icowIssL1 hphilyMID_allInNet_peace if(flag==1)
		est store net1

		suest beh1 net1	
	}

	// MODEL D.2
	{
		qui logit ccode1MID_allInNet 	contagionIss__allInNet ///
										$controlsBeh ///
															if(stateSample==1 & ccode1MID_allInNetL1!=. )
		est store beh1b_c2

		qui logit icowIss 	hphilyMID_allInNet_peace ///
							$controlsNet ///
												if(newGuys==0 & icowIssL1!=.)
		est store net1b_c2

		suest beh1b_c2 net1b_c2	
	}
	
	// MODEL D.3
	{	
		qui logit ccode1MID_allInNet ccode1MID_allInNetL1 pastMID_allInNet_5 contagionIss__allInNet $controlsBeh if(stateSample==1)
		est store beh1b_c2_a

		qui logit icowIss icowIssL1 hphilyMID_allInNet_peace $controlsNet if(newGuys==0)
		est store net1b_c2_a

		suest beh1b_c2_a net1b_c2_a	
	}
}
**************************
// Supplemental Table 6: Disaggregated Contagion - Sum Stats
{
	cd "$dir"
	use "data.dta", clear
	
	// get sample flags
	qui logit ccode1MID_allInNet ccode1MID_allInNetL1 contagionIss__allInNet $controlsBeh totalContig1 if(stateSample==1)
	cap drop bFlag
	gen bFlag = e(sample)

	// summarize the stats.
	qui{
		* BEH eq
		noi sum contagion?__allInNet if(bFlag==1)
	}
}
**************************
// Supplemental Table 7: Disaggregating Dispute Networks by Issue Type - Passive Self-Selection Only
{
	cd "$dir"
	use "data.dta", clear
	
	qui logit ccode1MID_allInNet ccode1MID_allInNetL1 contagionT__allInNet contagionR__allInNet contagionM__allInNet $controlsBeh if(stateSample==1)
	est store beh3a

	qui logit terrissMax terrissMaxL1 marissMaxL1 riverissMaxL1 hphilyMID_allInNet_peace $controlsNet if(newGuys==0)
	est store netT3a

	qui logit marissMax terrissMaxL1 marissMaxL1 riverissMaxL1  hphilyMID_allInNet_peace $controlsNet if(newGuys==0)
	est store netM3a

	qui logit riverissMax terrissMaxL1 marissMaxL1 riverissMaxL1 hphilyMID_allInNet_peace $controlsNet if(newGuys==0)
	est store netR3a

	suest beh3a netT3a netM3a netR3a
}
**************************
// Supplemental Table 8: Disaggregating Dispute Networks by Issue Type - Both Self-Selection Vars
{
	cd "$dir"
	use "data.dta", clear
	
	qui logit ccode1MID_allInNet ccode1MID_allInNetL1 contagionT__allInNet contagionR__allInNet contagionM__allInNet $controlsBeh if(stateSample==1)
	est store beh2

	qui logit terrissMax terrissMaxL1 marissMaxL1 riverissMaxL1 hphilyMID_allInNet_peace hphilyMID_allInNet_mid $controlsNet if(newGuys==0)
	est store netT2

	qui logit marissMax terrissMaxL1 marissMaxL1 riverissMaxL1  hphilyMID_allInNet_peace hphilyMID_allInNet_mid $controlsNet if(newGuys==0)
	est store netM2

	qui logit riverissMax terrissMaxL1 marissMaxL1 riverissMaxL1 hphilyMID_allInNet_peace hphilyMID_allInNet_mid $controlsNet if(newGuys==0)
	est store netR2

	suest beh2 netT2 netM2 netR2
}
*******************************************************************************	
// NUMBERS WITHIN TEXT
**************************
// Number of dyad-years with active disputed issue (Section II.B.1)
{
	cd "$dir"
	use "data.dta", clear
	
	// get sample flags
	qui logit icowIss icowIssL1 hphilyMID_allInNet_mid hphilyMID_allInNet_peace $controlsNet if(newGuys==0)
	cap drop flag
	gen flag = e(sample)
	
	tab icowIss if(flag==1)
}
**************************
// Number of country-years with MIDs in which country was aggressor (Section II.B.2)
{
	cd "$dir"
	use "data.dta", clear
	
	// get sample flags
	qui logit ccode1MID_allInNet ccode1MID_allInNetL1 contagionIss__allInNet $controlsBeh totalContig1 if(stateSample==1)
	cap drop bFlag
	gen bFlag = e(sample)
	
	tab ccode1MID_allInNet if(bFlag==1)
}
**************************
// In-text predicted probabilities from Model 1(Section III.A)
{
	* NOTE: canned version of Clarify doesn't include suest.  Implemented workaround.
	* 		See top of do-file for installation instructions for tweaked commands.
	* 		h/t to King, Tomz, and Wittenberg 2000; Tomz, Wittenberg, and King 2001.
	
	cd "$dir"
	use "data.dta", clear
		
		global behIVs ccode1MID_allInNetL1 contagionIss__allInNet $controlsBeh
		global netIVs icowIssL1 hphilyMID_allInNet_peace $controlsNet
	********************************************************************************************************
	// Estimate
	qui logit ccode1MID_allInNet $behIVs if(stateSample==1)
	est store beh1b_c2

	qui logit icowIss $netIVs if(newGuys==0)
	est store net1b_c2

	set seed 951623
	estsimp_skm suest beh1b_c2 net1b_c2, coev ///
		dv1(ccode1MID_allInNet) iv1($behIVs) ///
		dv2(icowIss) iv2($netIVs)
	

	// Everything at median for the dyadic sample
	* setx's being fussy about the correct median values for each subsample. So...
	foreach v of global behIVs{
		qui sum `v' if _est_beh1b_c==1, detail
		setx `v' `r(p50)'
	}
	foreach v of global netIVs{
		qui sum `v' if _est_net1b_c==1, detail
		setx `v' `r(p50)'
	}
	setx
	
	// NETWORK EQUATION
		// For initiation of dispute
		setx icowIssL1 0
					
		setx hphilyMID_allInNet_peace 0 // dyad contains one aggressive state
		simqi_skm // .0004891  

		setx hphilyMID_allInNet_peace 1 // both states in dyad are peaceful
		simqi_skm // .000344 

		display (  .0004891  -.000344  )/ .000344  	// = .42180233 (42.18% increase)	
		
	// BEHAVIOR EQUATION	
		// MID aggressor when not the prev year
		setx ccode1MID_allInNetL1 0
					
		setx contagionIss__allInNet 0 //entire network peaceful
		simqi_skm // .0597524 

		setx contagionIss__allInNet 0.02 //2% of network as MID aggressors
		simqi_skm // .0803144 

		display ( .0803144   - .0597524 )/ .0597524  	// = .34412007 (34.4% increase)
}
*******************************************************************************	
// FIGURES
**************************
// Figure 1: Dispute Network Map: WH and WE, 1950
{
	* NOTE: open/closed nodes + different widths for nodal connectivity done 
	*		manually with Graph Editor, plus other label positioning tweaks

	cd "$dir"
	use "data.dta", clear
	set matsize 11000
	
	cap drop anyIss
	cap drop anyIss1
	cap drop anyIss2
	cap drop ab1
	cap drop ab2
	cap drop _merge

	bysort ccode1Yr: egen anyIss1 = max(icowIss)
	bysort ccode2Yr: egen anyIss2 = max(icowIss)

	gen ab1 = abbrev1 if(icowIss==1 | (anyIss1==0 & stateSample==1))
	gen ab2 = abbrev2 if(icowIss==1)
	
	* The plot itself
	netplot ab1 ab2 if(year==1950), l type(circle) 

	
	* For node and line details:
	// Shaded vs. unshaded nodes: shaded = those that initiate a MID within the network in 1949
	tab ccode1 if(ccode1MID_allNet==1 & year==1949)
	
	// For line thickness
	egen totalIss = rowtotal(*issCount)
	sort totalIss
	list ab1 ab2 *issCount totalIss if(year==1950 & icowIss==1), noobs
}
*
